package com.hspedu.poly.polyarr;

public class PolyArray {
    public static void main(String[] args) {
        //创建数组
        Person[] person = new Person[5];
        //多态数组赋值
        person[0] = new Person("jack", 20);
        person[1] = new Student("mary", 18, 100);
        person[2] = new Student("smith", 19, 30.1);
        person[3] = new Teacher("scott", 30, 10000);
        person[4] = new Teacher("king", 50, 20000);

        //循环遍历多态数组，调用say()
        for (int i = 0; i < person.length; i++) {
            //person[i] 编译类型是 Person，运行类型是 Person or Student or Teacher
            System.out.println(person[i].say());
            //调用子类特有方法
            //判断对象运行类型，并向下转型
            if (person[i] instanceof Student) {
                ((Student)person[i]).study();
            } else if (person[i] instanceof Teacher) {
                ((Teacher)person[i]).teach();
            }
        }
    }
}

